package com.hh.workflow.service.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.json.Json;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.workflow.bean.HhWorkFlowInfo;

@Service
public class ActivitiService {
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	protected RuntimeService runtimeService;
	@Autowired
	protected IdentityService identityService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	protected TaskService taskService;
	@Autowired
	protected FormService formService;
	@Autowired
	protected ManagementService managementService;
	@Autowired
	private IHibernateDAO<HhWorkFlowInfo> hhWorkFlowInfoDAO;
	@Autowired
	protected IHibernateDAO<Object> sqldao;

	public void deploy(String dataId) throws Exception {
		InputStream is = null;
		try {
			// 得到配置的流程详细信息
			HhWorkFlowInfo workFlowInfo = hhWorkFlowInfoDAO.findEntity(
					HhWorkFlowInfo.class, Restrictions.eq("dataId", dataId));
			// 把图片字节转成流
			is = new ByteArrayInputStream(workFlowInfo.getActivitiImg());
			String actXml = workFlowInfo.getActivitiXml();
			Map<String, Object> rootMap = Json.toMap(workFlowInfo
					.getRootObject());
			String name = Convert.toString(rootMap.get("label"));
			if ("GBK".equals(System.getProperty("file.encoding"))) {
				this.repositoryService
						.createDeployment()
						.addString(name + ".bpmn20.xml",
								actXml.replace("UTF-8", "GBK"))
						.addInputStream(name + ".png", is).deploy();
			} else {
				// 部署一个流程
				this.repositoryService.createDeployment()
						.addString(name + ".bpmn20.xml", actXml)
						.addInputStream(name + ".png", is).deploy();
			}
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw e;
			}
		}
	}

	public List<Map<String, Object>> queryProcessDefinition() {
		List<ProcessDefinition> processDefinitions = this.repositoryService
				.createProcessDefinitionQuery().orderByProcessDefinitionVersion().desc().list();
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for (ProcessDefinition processDefinition : processDefinitions) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", processDefinition.getId());
			map.put("key", processDefinition.getKey());
			map.put("name", processDefinition.getName());
			map.put("deploymentId", processDefinition.getDeploymentId());
			map.put("description", processDefinition.getDescription());
			map.put("version", "版本："+processDefinition.getVersion());
			maps.add(map);
		}
		return maps;
	}

	public void deleteProcessDefinitions(String ids, boolean cascade) {
		List<String> idList = Convert.strToList(ids);
		for (String id : idList) {
			this.repositoryService.deleteDeployment(id, cascade);
		}
	}

	public RepositoryService getRepositoryService() {
		return repositoryService;
	}

	public void setRepositoryService(RepositoryService repositoryService) {
		this.repositoryService = repositoryService;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public IdentityService getIdentityService() {
		return identityService;
	}

	public void setIdentityService(IdentityService identityService) {
		this.identityService = identityService;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public ManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(ManagementService managementService) {
		this.managementService = managementService;
	}
	
}
